import {agentRequest, apiRequest, userRequest} from "@/utils/HttpRequest";


export async function findByAgentSelecttList() {
  //SwaggerResources


  return apiRequest<API.BaseArrayDataResult<API.SysConfig>> (
    "agent/findByBackSelectList", {
      method: "GET"
    } )
}

export async function findByAgentBySelectList() {
  return new Promise<API.GlobalStatus[]> ( (resolve, reject) => {
    agentRequest<API.BaseArrayDataResult<API.GlobalStatus>> (
      "findByBackSelectList", {
        method: "GET"
      } ).then ( (res) => {
      //   console.log("category/findBySelectList",res)
      resolve ( res?.data )
    } ).catch ( (e => {
      reject ( e )
    }) )
  } )
}

export async function findByAgentBySelecEemutList() {

  return new Promise<any> ( (resolve, reject) => {
    agentRequest<API.BaseArrayDataResult<API.GlobalStatus>> (
      "findByBackSelectList", {
        method: "GET"
      } ).then ( (res) => {
      //   console.log("category/findBySelectList",res)
      let data = res?.data;
      let dataarr = {}
      for (let i = 0; i < data?.length; i++) {
        let datum = data[i];
        datum['text']=datum['label']
        dataarr[datum['value']] = datum;

      }
      resolve ( dataarr )
    } ).catch ( (e => {
      reject ( e )
    }) )
  } )
}

export async function findByPageList(params?: any) {
  //SwaggerResources


  return agentRequest<API.DataType<API.Agent>> (
    "findByPageList", {
      method: "GET", params: {...params}
    } )


}

//editAgent
export async function editAgent(params: API.Category) {
  return apiRequest<API.BaseResult> ( "agent/edit", {
    method: "POST",
    data: params
  } )
}


//invitationEdit
export async function editInvitation(params: API.Category) {
  return apiRequest<API.BaseResult> ( "agent/invitationEdit", {
    method: "POST",
    data: params
  } )
}


export async function findByPageInvitationList(params?: any) {
  //SwaggerResources


  return agentRequest<API.BaseArrayDataResult<API.Agent>> (
    "findByPageInvitationList", {
      method: "GET", params: {...params}
    } )


}

//findByMiniSerachList

export async function getByMiniSerachList(type: number = 0) {
  return new Promise<API.AttributeValue[]> ( (resolve, reject) => {
    findBySelectAgentValueList ( type ).then ( (result) => {
      let a = result.data;
      let b = {};
      for (let i = 0; i < a.length; i++) {
        let aElement = a[i];
        b[aElement['value']] = aElement;
      }
      resolve ( b );
    } ).catch ( e => {
      reject ( e )
    } )

  } );
}

export async function findBySelectAgentValueList(type: number = 0) {
  //SwaggerResources
  let attrkey = "agentkey" + type + "NUmber";
  return new Promise<API.BaseArrayDataResult<any>> ( (resolve, reject) => {
    let items = localStorage.getItem ( attrkey );
    if (items) {

      var parse = JSON.parse ( items );

      resolve ( parse )
    } else {
      let url = "findByMiniSerachList";
      agentRequest<API.BaseArrayDataResult<any>> (
        url, {
          method: "GET",
          params: {type: type}
        } ).then ( (res) => {
        localStorage.setItem ( attrkey, JSON.stringify ( res ) )
        resolve ( res )
      } ).catch ( e => {
        reject ( e )
      } )
    }
  } )

}

//findByPageBackAgentReChangeList
export async function findByPageBackAgentReChangeList(params?: any) {
  //SwaggerResources

  return agentRequest<API.BaseArrayDataResult<any>> (
    "findByPageBackAgentReChangeList", {
      method: "GET", params: {...params}
    } )


}
export  function findByMiniAgentList(userId:number) {
  //SwaggerResources

  return agentRequest<API.BaseDataResult<API.GlobalStastic>> (
    "findByMiniList", {
      method: "GET", params: {userId:userId}
    } )


}







